home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / lpd / RCS / displayq.c,v < prev    next >
Encoding:
Text File  |  1989-08-16  |  10.0 KB  |  502 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     89.01.11.08.50.52;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.11.23.10.33.56;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @/*
  30.  * Copyright (c) 1983 Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms are permitted
  34.  * provided that this notice is preserved and that due credit is given
  35.  * to the University of California at Berkeley. The name of the University
  36.  * may not be used to endorse or promote products derived from this
  37.  * software without specific prior written permission. This software
  38.  * is provided ``as is'' without express or implied warranty.
  39.  */
  40.  
  41. #ifndef lint
  42. static char sccsid[] = "@@(#)displayq.c    5.7 (Berkeley) 6/1/88";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * Routines to display the state of the queue.
  47.  */
  48.  
  49. #include "lp.h"
  50.  
  51. #define JOBCOL    40        /* column for job # in -l format */
  52. #define OWNCOL    7        /* start of Owner column in normal */
  53. #define SIZCOL    62        /* start of Size column in normal */
  54.  
  55. /*
  56.  * Stuff for handling job specifications
  57.  */
  58. extern char    *user[];    /* users to process */
  59. extern int    users;        /* # of users in user array */
  60. extern int    requ[];        /* job number of spool entries */
  61. extern int    requests;    /* # of spool requests */
  62.  
  63. int    lflag;        /* long output option */
  64. char    current[40];    /* current file being printed */
  65. int    garbage;    /* # of garbage cf files */
  66. int    rank;        /* order to be printed (-1=none, 0=active) */
  67. long    totsize;    /* total print job size in bytes */
  68. int    first;        /* first file in ``files'' column? */
  69. int    col;        /* column on screen */
  70. int    sendtorem;    /* are we sending to a remote? */
  71. char    file[132];    /* print file name */
  72.  
  73. char    *head0 = "Rank   Owner      Job  Files";
  74. char    *head1 = "Total Size\n";
  75.  
  76. /*
  77.  * Display the current state of the queue. Format = 1 if long format.
  78.  */
  79. displayq(format)
  80.     int format;
  81. {
  82.     register struct queue *q;
  83.     register int i, nitems, fd;
  84.     register char    *cp;
  85.     struct queue **queue;
  86.     struct stat statb;
  87.     FILE *fp;
  88.     char c;
  89.  
  90.     lflag = format;
  91.     totsize = 0;
  92.     rank = -1;
  93.  
  94.     if ((i = pgetent(line, printer)) < 0)
  95.         fatal("cannot open printer description file");
  96.     else if (i == 0)
  97.         fatal("unknown printer");
  98. #ifndef sprite
  99.     if ((LP = pgetstr("lp", &bp)) == NULL)
  100.         LP = DEFDEVLP;
  101. #endif
  102.     if ((RP = pgetstr("rp", &bp)) == NULL)
  103.         RP = DEFLP;
  104.     if ((SD = pgetstr("sd", &bp)) == NULL)
  105.         SD = DEFSPOOL;
  106.     if ((LO = pgetstr("lo", &bp)) == NULL)
  107.         LO = DEFLOCK;
  108.     if ((ST = pgetstr("st", &bp)) == NULL)
  109.         ST = DEFSTAT;
  110.     RM = pgetstr("rm", &bp);
  111.  
  112.     /*
  113.      * Figure out whether the local machine is the same as the remote 
  114.      * machine entry (if it exists).  If not, then ignore the local
  115.      * queue information.
  116.      */
  117. #ifdef sprite
  118.     /*
  119.      * For sprite, look for a remote machine entry only if there
  120.      * is no local printer.
  121.      */
  122.     if ((LP = pgetstr("lp", &bp)) == NULL || *LP == '\0') {
  123.         LP = DEFDEVLP;
  124. #endif
  125.         if (RM != (char *) NULL) {
  126.         char name[256];
  127.         struct hostent *hp;
  128.  
  129.         /* get the standard network name of the local host */
  130.         gethostname(name, sizeof(name));
  131.         name[sizeof(name)-1] = '\0';
  132.         hp = gethostbyname(name);
  133.         if (hp == (struct hostent *) NULL) {
  134.             printf("unable to get network name for local machine %s\n",
  135.             name);
  136.             goto localcheck_done;
  137.         } else (void) strcpy(name, hp->h_name);
  138.  
  139.         /* get the network standard name of RM */
  140.         hp = gethostbyname(RM);
  141.         if (hp == (struct hostent *) NULL) {
  142.             printf("unable to get hostname for remote machine %s\n",
  143.             RM);
  144.             goto localcheck_done;
  145.         }
  146.  
  147.         /* if printer is not on local machine, ignore LP */
  148.         if (strcmp(name, hp->h_name)) {
  149.             LP = "";
  150.             ++sendtorem;
  151.         }
  152.         }
  153.     }
  154. localcheck_done:
  155.  
  156.     /*
  157.      * Print out local queue
  158.      * Find all the control files in the spooling directory
  159.      */
  160.     if (chdir(SD) < 0)
  161.         fatal("cannot chdir to spooling directory");
  162.     if ((nitems = getq(&queue)) < 0)
  163.         fatal("cannot examine spooling area\n");
  164.     if (stat(LO, &statb) >= 0) {
  165.         if (statb.st_mode & 0100) {
  166.             if (sendtorem)
  167.                 printf("%s: ", host);
  168.             printf("Warning: %s is down: ", printer);
  169.             fd = open(ST, O_RDONLY);
  170.             if (fd >= 0) {
  171.                 (void) flock(fd, LOCK_SH);
  172.                 while ((i = read(fd, line, sizeof(line))) > 0)
  173.                     (void) fwrite(line, 1, i, stdout);
  174.                 (void) close(fd);    /* unlocks as well */
  175.             } else
  176.                 putchar('\n');
  177.         }
  178.         if (statb.st_mode & 010) {
  179.             if (sendtorem)
  180.                 printf("%s: ", host);
  181.             printf("Warning: %s queue is turned off\n", printer);
  182.         }
  183.     }
  184.  
  185.     if (nitems) {
  186.         fp = fopen(LO, "r");
  187.         if (fp == NULL)
  188.             warn();
  189.         else {
  190.             register char *cp;
  191.  
  192.             /* get daemon pid */
  193.             cp = current;
  194.             while ((*cp = getc(fp)) != EOF && *cp != '\n')
  195.                 cp++;
  196.             *cp = '\0';
  197.             i = atoi(current);
  198.             if (i <= 0 || kill(i, 0) < 0)
  199.                 warn();
  200.             else {
  201.                 /* read current file name */
  202.                 cp = current;
  203.                 while ((*cp = getc(fp)) != EOF && *cp != '\n')
  204.                     cp++;
  205.                 *cp = '\0';
  206.                 /*
  207.                  * Print the status file.
  208.                  */
  209.                 if (sendtorem)
  210.                     printf("%s: ", host);
  211.                 fd = open(ST, O_RDONLY);
  212.                 if (fd >= 0) {
  213.                     (void) flock(fd, LOCK_SH);
  214.                     while ((i = read(fd, line, sizeof(line))) > 0)
  215.                         (void) fwrite(line, 1, i, stdout);
  216.                     (void) close(fd);    /* unlocks as well */
  217.                 } else
  218.                     putchar('\n');
  219.             }
  220.             (void) fclose(fp);
  221.         }
  222.         /*
  223.          * Now, examine the control files and print out the jobs to
  224.          * be done for each user.
  225.          */
  226.         if (!lflag)
  227.             header();
  228.         for (i = 0; i < nitems; i++) {
  229.             q = queue[i];
  230.             inform(q->q_name);
  231.             free(q);
  232.         }
  233.         free(queue);
  234.     }
  235.     if (!sendtorem) {
  236.         if (nitems == 0)
  237.             puts("no entries");
  238.         return;
  239.     }
  240.  
  241.     /*
  242.      * Print foreign queue
  243.      * Note that a file in transit may show up in either queue.
  244.      */
  245.     if (nitems)
  246.         putchar('\n');
  247.     (void) sprintf(line, "%c%s", format + '\3', RP);
  248.     cp = line;
  249.     for (i = 0; i < requests; i++) {
  250.         cp += strlen(cp);
  251.         (void) sprintf(cp, " %d", requ[i]);
  252.     }
  253.     for (i = 0; i < users; i++) {
  254.         cp += strlen(cp);
  255.         *cp++ = ' ';
  256.         (void) strcpy(cp, user[i]);
  257.     }
  258.     strcat(line, "\n");
  259.     fd = getport(RM);
  260.     if (fd < 0) {
  261.         if (from != host)
  262.             printf("%s: ", host);
  263.         printf("connection to %s is down: %s\n", RM, strerror(errno));
  264.     } else {
  265.         i = strlen(line);
  266.         if (write(fd, line, i) != i)
  267.             fatal("Lost connection");
  268.         while ((i = read(fd, line, sizeof(line))) > 0)
  269.             (void) fwrite(line, 1, i, stdout);
  270.         (void) close(fd);
  271.     }
  272. }
  273.  
  274. /*
  275.  * Print a warning message if there is no daemon present.
  276.  */
  277. warn()
  278. {
  279.     if (sendtorem)
  280.         printf("\n%s: ", host);
  281.     puts("Warning: no daemon present");
  282.     current[0] = '\0';
  283. }
  284.  
  285. /*
  286.  * Print the header for the short listing format
  287.  */
  288. header()
  289. {
  290.     printf(head0);
  291.     col = strlen(head0)+1;
  292.     blankfill(SIZCOL);
  293.     printf(head1);
  294. }
  295.  
  296. inform(cf)
  297.     char *cf;
  298. {
  299.     register int j, k;
  300.     register char *cp;
  301.     FILE *cfp;
  302.  
  303.     /*
  304.      * There's a chance the control file has gone away
  305.      * in the meantime; if this is the case just keep going
  306.      */
  307.     if ((cfp = fopen(cf, "r")) == NULL)
  308.         return;
  309.  
  310.     if (rank < 0)
  311.         rank = 0;
  312.     if (sendtorem || garbage || strcmp(cf, current))
  313.         rank++;
  314.     j = 0;
  315.     while (getline(cfp)) {
  316.         switch (line[0]) {
  317.         case 'P': /* Was this file specified in the user's list? */
  318.             if (!inlist(line+1, cf)) {
  319.                 fclose(cfp);
  320.                 return;
  321.             }
  322.             if (lflag) {
  323.                 printf("\n%s: ", line+1);
  324.                 col = strlen(line+1) + 2;
  325.                 prank(rank);
  326.                 blankfill(JOBCOL);
  327.                 printf(" [job %s]\n", cf+3);
  328.             } else {
  329.                 col = 0;
  330.                 prank(rank);
  331.                 blankfill(OWNCOL);
  332.                 printf("%-10s %-3d  ", line+1, atoi(cf+3));
  333.                 col += 16;
  334.                 first = 1;
  335.             }
  336.             continue;
  337.         default: /* some format specifer and file name? */
  338.             if (line[0] < 'a' || line[0] > 'z')
  339.                 continue;
  340.             if (j == 0 || strcmp(file, line+1) != 0)
  341.                 (void) strcpy(file, line+1);
  342.             j++;
  343.             continue;
  344.         case 'N':
  345.             show(line+1, file, j);
  346.             file[0] = '\0';
  347.             j = 0;
  348.         }
  349.     }
  350.     fclose(cfp);
  351.     if (!lflag) {
  352.         blankfill(SIZCOL);
  353.         printf("%ld bytes\n", totsize);
  354.         totsize = 0;
  355.     }
  356. }
  357.  
  358. inlist(name, file)
  359.     char *name, *file;
  360. {
  361.     register int *r, n;
  362.     register char **u, *cp;
  363.  
  364.     if (users == 0 && requests == 0)
  365.         return(1);
  366.     /*
  367.      * Check to see if it's in the user list
  368.      */
  369.     for (u = user; u < &user[users]; u++)
  370.         if (!strcmp(*u, name))
  371.             return(1);
  372.     /*
  373.      * Check the request list
  374.      */
  375.     for (n = 0, cp = file+3; isdigit(*cp); )
  376.         n = n * 10 + (*cp++ - '0');
  377.     for (r = requ; r < &requ[requests]; r++)
  378.         if (*r == n && !strcmp(cp, from))
  379.             return(1);
  380.     return(0);
  381. }
  382.  
  383. show(nfile, file, copies)
  384.     register char *nfile, *file;
  385. {
  386.     if (strcmp(nfile, " ") == 0)
  387.         nfile = "(standard input)";
  388.     if (lflag)
  389.         ldump(nfile, file, copies);
  390.     else
  391.         dump(nfile, file, copies);
  392. }
  393.  
  394. /*
  395.  * Fill the line with blanks to the specified column
  396.  */
  397. blankfill(n)
  398.     register int n;
  399. {
  400.     while (col++ < n)
  401.         putchar(' ');
  402. }
  403.  
  404. /*
  405.  * Give the abbreviated dump of the file names
  406.  */
  407. dump(nfile, file, copies)
  408.     char *nfile, *file;
  409. {
  410.     register short n, fill;
  411.     struct stat lbuf;
  412.  
  413.     /*
  414.      * Print as many files as will fit
  415.      *  (leaving room for the total size)
  416.      */
  417.      fill = first ? 0 : 2;    /* fill space for ``, '' */
  418.      if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
  419.         if (col < SIZCOL) {
  420.             printf(" ..."), col += 4;
  421.             blankfill(SIZCOL);
  422.         }
  423.     } else {
  424.         if (first)
  425.             first = 0;
  426.         else
  427.             printf(", ");
  428.         printf("%s", nfile);
  429.         col += n+fill;
  430.     }
  431.     if (*file && !stat(file, &lbuf))
  432.         totsize += copies * lbuf.st_size;
  433. }
  434.  
  435. /*
  436.  * Print the long info about the file
  437.  */
  438. ldump(nfile, file, copies)
  439.     char *nfile, *file;
  440. {
  441.     struct stat lbuf;
  442.  
  443.     putchar('\t');
  444.     if (copies > 1)
  445.         printf("%-2d copies of %-19s", copies, nfile);
  446.     else
  447.         printf("%-32s", nfile);
  448.     if (*file && !stat(file, &lbuf))
  449.         printf(" %ld bytes", lbuf.st_size);
  450.     else
  451.         printf(" ??? bytes");
  452.     putchar('\n');
  453. }
  454.  
  455. /*
  456.  * Print the job's rank in the queue,
  457.  *   update col for screen management
  458.  */
  459. prank(n)
  460. {
  461.     char line[100];
  462.     static char *r[] = {
  463.         "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
  464.     };
  465.  
  466.     if (n == 0) {
  467.         printf("active");
  468.         col += 6;
  469.         return;
  470.     }
  471.     if ((n/10) == 1)
  472.         (void) sprintf(line, "%dth", n);
  473.     else
  474.         (void) sprintf(line, "%d%s", n, r[n%10]);
  475.     col += strlen(line);
  476.     printf("%s", line);
  477. }
  478. @
  479.  
  480.  
  481. 1.1
  482. log
  483. @Initial revision
  484. @
  485. text
  486. @d70 1
  487. d73 1
  488. d89 9
  489. a97 1
  490.      if (RM != (char *) NULL) {
  491. d121 2
  492. a122 2
  493.             *LP = '\0';
  494.             ++sendtorem;
  495. d124 1
  496. d235 2
  497. a236 3
  498.         printf("connection to %s is down\n", RM);
  499.     }
  500.     else {
  501. @
  502.